package ui.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import org.greenrobot.eventbus.EventBus;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Utils.CheckPhoneNumberUtils;
import Utils.SharedPrefStore;
import base.BaseBiz;
import base.BaseFra;
import butterknife.BindView;
import butterknife.ButterKnife;
import coom.MyType;
import coom.RetrofitHelp;
import event.CloseActEvent;
import event.MessageEvent;
import event.NetErrorEvent;
import model.PersonalCenterBean;
import ui.activity.login.LoginAct;
import ui.activity.mine.SettingAct;
import ui.activity.mine.biz.MineBiz;
import ui.activity.mine.component.DaggerMineComponent;
import ui.activity.mine.contract.MineContract;
import ui.activity.mine.module.MineModule;
import ui.activity.mine.presenter.MinePresenter;
import ui.activity.profit.biz.ProfitBiz;
import util.AlertUtils;
import util.StringUtils;
import widget.alertview.AlertView;

/**
 * 个人中心
 */
public class MyFragment extends BaseFra implements MineContract.View {


    @Inject
    MinePresenter presenter;
    @Inject
    MineBiz biz;

    @BindView(R.id.fragment_my_image)
    SimpleDraweeView myImage;
    @BindView(R.id.user_real_name)
    TextView realName;
    @BindView(R.id.user_phone)
    TextView phone;
    @BindView(R.id.fragment_my_linear)
    LinearLayout myLinear;
    @BindView(R.id.arrow_right)
    ImageView shareRight;
    @BindView(R.id.fragment_my_toshare)
    ImageView share;
    @BindView(R.id.order_center_layout)
    RelativeLayout orderCenter;
    @BindView(R.id.fragment_my_relative_address)
    RelativeLayout address;
    @BindView(R.id.fragment_my_issm)
    TextView myIssm;
    @BindView(R.id.auth_layout)
    RelativeLayout authLayout;
    @BindView(R.id.fragment_my_relative_mywallet)
    RelativeLayout mywallet;
    @BindView(R.id.fragment_my_relative_messagecenter)
    RelativeLayout messagecenter;
    @BindView(R.id.fragment_my_complaint)
    RelativeLayout complaint;
    @BindView(R.id.fragment_my_relative_tixian)
    RelativeLayout fragment_my_relative_tixian;
    @BindView(R.id.fragment_my_relative_support)
    RelativeLayout fragment_my_relative_support;
    @BindView(R.id.fragment_my_moreproblemem)
    RelativeLayout fragment_my_moreproblemem;
    @BindView(R.id.fragment_my_aboutus)
    RelativeLayout aboutus;
    @BindView(R.id.fragment_my_setting)
    RelativeLayout setting;
    @BindView(R.id.fragment_my_my_redball)
    RelativeLayout fragment_my_my_redball;
    @BindView(R.id.fragment_my_yinsi)
    RelativeLayout fragment_my_yinsi;


    @BindView(R.id.message_number)
    TextView messageNumber;

    private SharedPreferences sp;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        ButterKnife.bind(this, view);
        DaggerMineComponent.builder().mineModule(new MineModule(this)).build().inject(this);
        presenter.setBiz(biz);
        SharedPrefStore store = new SharedPrefStore();
        RxView.clicks(myLinear)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toUser());
        RxView.clicks(shareRight)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toShare());
        RxView.clicks(share)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toShare());
        RxView.clicks(orderCenter)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toOrder());
        RxView.clicks(address)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toAddress());
        RxView.clicks(authLayout)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toAuthentication());
        RxView.clicks(mywallet)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toWallet());

        RxView.clicks(fragment_my_relative_tixian)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toTiXian());

        RxView.clicks(fragment_my_relative_support)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toSupportOnline());

        RxView.clicks(fragment_my_moreproblemem)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toMoreSupport());

        RxView.clicks(messagecenter)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toMessage());
        RxView.clicks(complaint)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toComplaint());
        RxView.clicks(aboutus)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toAbout());
        RxView.clicks(setting)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toSetting());
        RxView.clicks(fragment_my_my_redball)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toRedball());

        RxView.clicks(fragment_my_yinsi)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toyinsi());
        return view;
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        biz.onDestory();
        ButterKnife.bind(getActivity()).unbind();
    }


    @Override
    public void onResume() {
        super.onResume();
        try {
            sp = getActivity().getSharedPreferences("ToBuy", Context.MODE_PRIVATE);
            presenter.getUserIncome(sp.getString("id", "0"));
        } catch (Exception e) {
        }
    }


    @Override
    public void upDateUI(PersonalCenterBean.DataBean bean) {
//        if (!StringUtils.isEmpty(bean.getCustom().getHeadImage())) {
//            myImage.setImageURI(Uri.parse(bean.getCustom().getHeadImage()));
//        }
        if (!TextUtils.isEmpty(bean.getCustom().getCustomName())) {
            phone.setText(CheckPhoneNumberUtils.hideCardNo(bean.getCustom().getCustomName()));
            phone.setVisibility(View.VISIBLE);
        }
        if (bean.getCustom().getIsRealNameAuthenticationed() == 1) {
            myIssm.setText("已实名");
            SharedPrefStore store = new SharedPrefStore();
            store.putString("isRealNameAuthenticationed", "1");
        } else {
            myIssm.setText("未实名");
        }
        realName.setText(StringUtils.isEmpty(bean.getCustom().getRealName())?"好钻钱":bean.getCustom().getRealName());
        EventBus.getDefault().post(new MessageEvent(bean.getReadnum()));
        if (bean.getReadnum() != 0) {
            messageNumber.setVisibility(View.VISIBLE);
            if (bean.getReadnum() > 9) {
                messageNumber.setText("9+");
            } else {
                messageNumber.setText(bean.getReadnum() + "");
            }
        } else {
            messageNumber.setVisibility(View.INVISIBLE);
        }


    }
}
